import { Moment } from 'moment';
import { getAppListByTenantCode, getComponentListByAppId } from '/@/api/maintenance-center/system-log/systemLogs';

export const columns = [
  {
    title: '操作人ID',
    dataIndex: 'userId',
    width: '100px',
  },
  {
    title: '操作名称',
    dataIndex: 'operationName',
    width: '100px',
  },
  {
    title: '操作描述',
    dataIndex: 'description',
    width: '100px',
  },
  {
    title: '请求方法',
    dataIndex: 'method',
    width: '100px',
  },
  {
    title: '请求参数',
    dataIndex: 'params',
    width: '100px',
    customCell: () => {
      return {
        style: {
          wordWrap: 'break-word',
          wordBreak: 'break-all',
          whiteSpace: 'normal',
          minHeight: '50px',
          width: '100px',
        },
      };
    },
  },
  {
    title: '请求URL',
    dataIndex: 'uri',
    width: '100px',
    customCell: () => {
      return {
        style: {
          wordWrap: 'break-word',
          wordBreak: 'break-all',
          whiteSpace: 'normal',
          minHeight: '50px',
          width: '100px',
        },
      };
    },
  },
  {
    title: '操作源IP',
    dataIndex: 'srcIp',
    width: '100px',
  },
  {
    title: '发生时间',
    dataIndex: 'logTime',
    customRender: (record) => {
      return record.value.replace('T', ' ');
    },
    customCell: () => {
      return {
        style: {
          wordWrap: 'break-word',
          wordBreak: 'break-all',
          whiteSpace: 'normal',
          minHeight: '50px',
          width: '100px',
        },
      };
    },
    width: '100px',
  },
  {
    title: '耗时(ms)',
    dataIndex: 'duration',
    width: '100px',
  },
  {
    title: '返回码/请求结果',
    dataIndex: 'status',
    width: '100px',
  },
];

export const searchFormSchema = [
  {
    field: 'limit',
    label: '日志条数',
    required: true,
    component: 'Input',
    // defaultValue: "5",
    rules: [
      {
        trigger: 'change',
        required: true,
        message: '请输入日志条数',
      },
      {
        trigger: 'change',
        pattern: /^(?:[1-9]|\d{2,3}|[1-4]\d{3}|5000)$/,
        message: '请输入不大于5000条数',
      },
    ],
    componentProps: {
      placeholder: '请输入日志条数',
    },
    labelWidth: 100,
    colProps: {
      span: 6,
    },
  },
  {
    field: 'logTime',
    label: '发生时间',
    component: 'RangePicker',
    required: true,
    componentProps: {
      placeholder: '请选择发生时间',
      showTime: true,
      disabledDate: (current: Moment) => {
        const dates = new Date().toLocaleDateString();
        const diffDate = current.diff(dates, 'days');
        return diffDate < -30 || diffDate > 0;
      },
    },
    labelWidth: 100,
    colProps: {
      span: 6,
    },
  },
  {
    field: 'keyword',
    label: '关键词',
    component: 'Input',
    componentProps: {
      placeholder: '请输入关键词',
    },
    labelWidth: 100,
    colProps: {
      span: 6,
    },
  },
  {
    field: 'appId',
    label: '应用',
    component: 'ApiSelect',
    componentProps: ({ formActionType }) => {
      return {
        placeholder: '请选择应用',
        api: getAppListByTenantCode,
        resultField: 'list',
        labelField: 'appName',
        params: {
          tenantCode: '',
        },
        valueField: 'appId',
        onChange: async (e) => {
          const { setFieldsValue, updateSchema } = formActionType;
          setFieldsValue({
            appId: undefined,
            appName: undefined,
          });
          updateSchema({
            field: 'module',
            label: '组件',
            component: 'ApiSelect',
            dynamicDisabled: e ? false : true,
            labelWidth: 100,
            colProps: {
              span: 6,
            },
            componentProps: {
              placeholder: '请选择组件',
              api: getComponentListByAppId,
              params: {
                appId: e,
              },
              resultField: 'list',
              labelField: 'componentName',
              valueField: 'componentName',
            },
          });
        },
      };
    },
    labelWidth: 100,
    colProps: {
      span: 6,
    },
  },
  {
    field: 'module',
    label: '组件',
    component: 'ApiSelect',
    componentProps: {
      placeholder: '请选择组件',
    },
    dynamicDisabled: true,
    labelWidth: 100,
    colProps: {
      span: 6,
    },
  },
];
